package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.query.DestinationSearchQuery;
import cn.wolfcode.luowowo.search.repository.IDestinationDetailTemplateRepository;
import cn.wolfcode.luowowo.search.service.IDestinationDetailTemplateSearchService;
import cn.wolfcode.luowowo.search.template.DestinationDetailTemplate;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import com.alibaba.dubbo.config.annotation.Service;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DestinationDetailTemplateSearchServiceImpl implements IDestinationDetailTemplateSearchService {

    @Autowired
    private IDestinationDetailTemplateRepository repository;

    @Autowired
    private ElasticsearchTemplate template;

    @Autowired
    private TransportClient client;

    @Override
    public void saveOrUpdate(DestinationDetailTemplate destinationTemplate) {

        repository.save(destinationTemplate);
    }

   @Override
    public List<StatisVO> groupByTypeId(Long typeId) {
        Iterable<DestinationDetailTemplate> iterable = repository.search(QueryBuilders.termQuery("typeId", typeId));
        List<StatisVO> list = new ArrayList<>();

        //第三个完善:目的地去除重复
        List<String> temp = new ArrayList<>(); //放置已经存在的目的地名称
        iterable.forEach(st -> {

            //如果临时的list中不存在这个目的地名,执行添加操作
            if (!temp.contains(st.getThemeName())) {
                StatisVO vo = new StatisVO();
                vo.setId(st.getThemeId());
                vo.setName(st.getThemeName());
                list.add(vo);

                //添加完成之后,临时list必须将该目的地名保存
                temp.add(st.getThemeName());
            }
        });

        return list;
    }

    @Override
    public Page searchData(Map<String, Long> map, DestinationSearchQuery qo) {
        //搜索条件
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        for (String s : map.keySet()) {
            TermQueryBuilder query = QueryBuilders.termQuery(s, map.get(s));

            boolQueryBuilder.must(query);
        }
       builder.withQuery(boolQueryBuilder);
        //分页
        builder.withPageable(qo.getPageable());
        //设置索引和类型
        builder.withIndices(DestinationDetailTemplate.INDEX_NAME);
        builder.withTypes(DestinationDetailTemplate.TYPE_NAME);
        Page<DestinationDetailTemplate> page = repository.search(builder.build());
        return page;
    }
}
